package cc.rengu.igas.channel.esb.biz.service.impl;

import cc.rengu.igas.channel.esb.common.constant.EsbServiceConstant;
import cc.rengu.igas.channel.esb.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.esb.facade.QuickPayService;
import cc.rengu.igas.channel.esb.facade.request.*;
import cc.rengu.igas.channel.esb.facade.response.*;
import cc.rengu.igas.channel.esb.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by wangcheng
 * 快捷支付实现类
 *
 * @author wangcheng
 */
public class QuickPayServiceImpl implements QuickPayService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<DebitCardQuickPayResponse> debitCardQuickPay(DebitCardQuickPayRequest debitCardQuickPayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<DebitCardQuickPayResponse> result = new Result<>();
        DebitCardQuickPayResponse debitCardQuickPayResponse = new DebitCardQuickPayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(debitCardQuickPayRequest, EsbServiceConstant.DEBIT_CARD_QUICK_PAY_SERVICE, DebitCardQuickPayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((DebitCardQuickPayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行借记卡快捷支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                debitCardQuickPayResponse.setRespCode(((BizException) e).getCode());
                debitCardQuickPayResponse.setRespDesc(e.getMessage());
            } else {
                debitCardQuickPayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                debitCardQuickPayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<DebitCardQuickPayStatusQueryResponse> debitCardQuickPayStatusQuery(DebitCardQuickPayStatusQueryRequest debitCardQuickPayStatusQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<DebitCardQuickPayStatusQueryResponse> result = new Result<>();
        DebitCardQuickPayStatusQueryResponse debitCardQuickPayStatusQueryResponse = new DebitCardQuickPayStatusQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(debitCardQuickPayStatusQueryRequest, EsbServiceConstant.DEBIT_CARD_QUICK_PAY_STATUS_QUERY_SERVICE, DebitCardQuickPayStatusQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((DebitCardQuickPayStatusQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行借记卡快捷支付状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                debitCardQuickPayStatusQueryResponse.setRespCode(((BizException) e).getCode());
                debitCardQuickPayStatusQueryResponse.setRespDesc(e.getMessage());
            } else {
                debitCardQuickPayStatusQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                debitCardQuickPayStatusQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<DebitCardQuickPayRefundResponse> debitCardQuickPayRefund(DebitCardQuickPayRefundRequest debitCardQuickPayRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<DebitCardQuickPayRefundResponse> result = new Result<>();
        DebitCardQuickPayRefundResponse debitCardQuickPayRefundResponse = new DebitCardQuickPayRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(debitCardQuickPayRefundRequest, EsbServiceConstant.DEBIT_CARD_QUICK_PAY_REFUND_SERVICE, DebitCardQuickPayRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((DebitCardQuickPayRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行借记卡快捷支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                debitCardQuickPayRefundResponse.setRespCode(((BizException) e).getCode());
                debitCardQuickPayRefundResponse.setRespDesc(e.getMessage());
            } else {
                debitCardQuickPayRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                debitCardQuickPayRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CreditCardQuickPayResponse> creditCardQuickPay(CreditCardQuickPayRequest creditCardQuickPayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CreditCardQuickPayResponse> result = new Result<>();
        CreditCardQuickPayResponse creditCardQuickPayResponse = new CreditCardQuickPayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(creditCardQuickPayRequest, EsbServiceConstant.CREDIT_CARD_QUICK_PAY_SERVICE, CreditCardQuickPayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CreditCardQuickPayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行贷记卡快捷支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditCardQuickPayResponse.setRespCode(((BizException) e).getCode());
                creditCardQuickPayResponse.setRespDesc(e.getMessage());
            } else {
                creditCardQuickPayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditCardQuickPayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CreditCardQuickPayStatusQueryResponse> creditCardQuickPayStatusQuery(CreditCardQuickPayStatusQueryRequest creditCardQuickPayStatusQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CreditCardQuickPayStatusQueryResponse> result = new Result<>();
        CreditCardQuickPayStatusQueryResponse creditCardQuickPayStatusQueryResponse = new CreditCardQuickPayStatusQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(creditCardQuickPayStatusQueryRequest, EsbServiceConstant.CREDIT_CARD_QUICK_PAY_STATUS_QUERY_SERVICE, CreditCardQuickPayStatusQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CreditCardQuickPayStatusQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行贷记卡快捷支付状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditCardQuickPayStatusQueryResponse.setRespCode(((BizException) e).getCode());
                creditCardQuickPayStatusQueryResponse.setRespDesc(e.getMessage());
            } else {
                creditCardQuickPayStatusQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditCardQuickPayStatusQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CreditCardQuickPayRefundResponse> creditCardQuickPayRefund(CreditCardQuickPayRefundRequest creditCardQuickPayRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CreditCardQuickPayRefundResponse> result = new Result<>();
        CreditCardQuickPayRefundResponse creditCardQuickPayRefundResponse = new CreditCardQuickPayRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(creditCardQuickPayRefundRequest, EsbServiceConstant.CREDIT_CARD_QUICK_PAY_REFUND_SERVICE, CreditCardQuickPayRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CreditCardQuickPayRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("本行贷记卡快捷支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditCardQuickPayRefundResponse.setRespCode(((BizException) e).getCode());
                creditCardQuickPayRefundResponse.setRespDesc(e.getMessage());
            } else {
                creditCardQuickPayRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditCardQuickPayRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
